package setting

import (
	"encoding/json"
	"fmt"
	"time"
)

// ServerSettingS 服务相关配置
type ServerSettingS struct {
	RunMode        string
	HttpPort       string
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	ContextTimeout time.Duration
}

// Oss 相关配置
type Oss struct {
	AccessKeyID     string      `yaml:"accessKeyId"`
	AccessKeySecret string      `yaml:"accessKeySecret"`
	Endpoint        string      `yaml:"endpoint"`
	Bucket          string      `yaml:"bucket"`
	Domain          string      `yaml:"domain"`
	SecurityToken   interface{} `yaml:"securityToken"`
	Timeout         int         `yaml:"timeout"`
	RoleArn         string      `yaml:"roleArn"`
	Region          string      `yaml:"region"`
}

// Prometheus 相关配置
type Prometheus struct {
	Host        string `yaml:"host"`
	StoragePath string `yaml:"storagePath"`
}

// HTMLToImg 网页转图片服务域名
type HTMLToImg struct {
	Domain string `yaml:"domain"`
}

// DefaultRobot 默认机器人配置
type DefaultRobot struct {
	Robot     string   `yaml:"robot"`
	Token     string   `yaml:"token"`
	Secret    string   `yaml:"secret"`
	AtMobiles []string `yaml:"atMobiles"`
}

// Robot 机器人相关配置
type Robot []RobotConf

type RobotMap map[string]RobotConf

type RobotConf struct {
	AlertName string   `yaml:"alertName,omitempty"`
	Token     string   `yaml:"token,omitempty"`
	Secret    string   `yaml:"secret,omitempty"`
	AtMobiles []string `yaml:"atMobiles,omitempty"`
}

type AppSetting struct {
	LogSavePath string `yaml:"LogSavePath"`
	LogFileName string `yaml:"LogFileName"`
	LogFileExt  string `yaml:"LogFileExt"`
}

var sections = make(map[string]interface{})

func (s *Setting) ReloadAllSection() error {
	for k, v := range sections {
		err := s.ReadSection(k, v)
		if err != nil {
			return err
		}
	}

	bytes, _ := json.Marshal(sections)
	fmt.Printf("%+v\n", string(bytes))

	return nil
}

func (s *Setting) ReadSection(k string, v interface{}) error {
	err := s.vp.UnmarshalKey(k, v)
	if err != nil {
		return err
	}

	if _, ok := sections[k]; !ok {
		sections[k] = v
	}

	return nil
}
